package org.brunoc.searchstores4j.vo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.brunoc.searchstores4j.SupportedStores;
import org.brunoc.searchstores4j.util.HttpClientFactory;
import org.brunoc.searchstores4j.vo.models.IStoreObject;
import org.brunoc.searchstores4j.vo.models.Product;

import com.googlecode.inutils4j.MyStringUtils;

/**
 * Class that implements search via HyperText Transfer Protocol at Americanas.com
 * @author Bruno Candido Volpato da Cunha
 *
 */
public class AmericanasStore extends IStoreObject {

	private static final Long AFFILIATED_ID = 999999L;
	
	public AmericanasStore() {
		this.name = "Americanas";
		this.searchUrl = "http://www.americanas.com.br/busca/[#SEARCH]?search.limit=[#LIMIT]&search.dir=desc&p0=busca&_PROFILE=buscaB&q=[#SEARCH]&p1=[#SEARCH]&term=[#SEARCH]&search.order=score&search.offset=[#OFFSET]";
		this.store = SupportedStores.AMERICANAS;
	}
	
	private boolean supportLimit = true;
	
	
	public String makeUrl(String query, int offset) {
		String urlRet = searchUrl.replace("[#SEARCH]", query);
		urlRet = urlRet.replace("[#OFFSET]", String.valueOf(offset));
		
		if (supportLimit) {
			urlRet = urlRet.replace("[#LIMIT]", String.valueOf(20));
		}
		
		return urlRet; 
	}


	@Override
	public List<Product> search(String url) throws HttpException, IOException {
		List<Product> productList = new ArrayList<Product>();
		
		String content = MyStringUtils.getContent(HttpClientFactory.getInstance(), url);
		
		//if (content.contains("\"wBox list\"")) {
		//content = content.split("<div class=\"wBox list\">")[1].trim();
		//content = content.split("<div class=\"rSearch showBox showBox02\">")[1].trim();

		content = content.replaceAll("\t", " ").trim();
		content = content.replaceAll("  ", " ").trim();
		
		String[] productSplit = content.split("<div class=\"hproduct\">");
		
		for (String productStructure : productSplit) {
			if (!productStructure.contains(">Por: R$")) continue;
			
			Product product = new Product(this.store);
			
			String name = MyStringUtils.regexFindFirst("<strong class=\"n name fn\">(.*)</strong>", productStructure);
			//String brand = MyStringUtils.regexFindFirst("<span class=\"brand\"><strong>(.*)</strong></span>", productStructure);
			//String description = MyStringUtils.regexFindFirst("[\\f,\\n,\\r,\\W\\D\\d]+(<span class=\"description\">(.{0,})</span>)[\\f,\\n,\\r,\\W\\D\\d]+", productStructure);
			//String description = MyStringUtils.regexFindFirst("<span class=\"description\">(.*)</span>", productStructure);
			String image = MyStringUtils.regexFindFirst("src=\"(.*)\" longdesc=\"", productStructure);
			String price = MyStringUtils.regexFindFirst("<span class=\"sale price\">Por: (.*)</span>", productStructure);
			String productUrl = MyStringUtils.regexFindFirst("<a href=\"(.*)\" class=\"url\" rel=\"product\">", productStructure);
			
			product.setName(name);
			//product.setBrand(brand);
			//product.setDescription(description);
			product.setImageUrl(image);
			
			
			String priceReplace = price.replace("R$", "");
			priceReplace = priceReplace.replace(".", "");
			priceReplace = priceReplace.replace(",", ".");
			
			product.setPrice(Double.valueOf(priceReplace));
			product.setProductUrl(productUrl + "?franq=" + AFFILIATED_ID);
			
			/*System.out.println("**************************");
			System.out.println("Name: " + product.getName());
			System.out.println("Brand: " + product.getBrand());
			System.out.println("Description: " + product.getDescription());
			System.out.println("Image: " + product.getImageUrl());
			System.out.println("Price: " + product.getPrice());
			System.out.println("Product URL: " + product.getProductUrl());
					*/
			
			productList.add(product);
		}	
		
		return productList;
	}

	
	
}
